package grpc

type Server struct {
	Name     string
	Api      []*Api
	InjectTo []string //将gprc代码注入到指定模块内
}

func CreateServer(name string) *Server {
	server := &Server{
		Name:     name,
		Api:      make([]*Api, 0),
		InjectTo: make([]string, 0),
	}
	//添加一些默认的GRPC接口服务，默认提供
	//心跳接口
	HeartBeat(server)
	//广播监听接口
	BroadcastListen(server)
	return server
}

func (s *Server) AddApi(name string) *Api {
	api := createApi(name)
	s.Api = append(s.Api, api)
	return api
}
func (s *Server) AddToTargetProject(name string) {
	s.InjectTo = append(s.InjectTo, name)
}

func (c *Server) Generate() {
	CreateProto(c)
	CreateServerFile(c, c.InjectTo...)
	CreateClientFile(c, c.InjectTo...)
}

type Api struct {
	Name     string
	Describe string
	InPut    []Field
	OutPut   []Field
}

func NewApi() *Api {
	return &Api{}
}

func createApi(name string) *Api {
	c := NewApi()
	c.Name = name
	c.InPut = make([]Field, 0)
	c.OutPut = make([]Field, 0)
	return c
}

func (c *Api) AddIn(name string) Field {
	f := NewField(name)
	c.InPut = append(c.InPut, f)
	return f
}
func (c *Api) AddOut(name string) Field {
	f := NewField(name)
	c.OutPut = append(c.OutPut, f)
	return f
}
func (c *Api) AddDescribe(content string) *Api {
	c.Describe = content
	return c
}
